package com.ra.service.impl;

import com.alibaba.fastjson.support.spring.annotation.FastJsonFilter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ra.exception.BizException;
import com.ra.mapper.RoleMapper;
import com.ra.mapper.UserAuthMapper;
import com.ra.mapper.UserInfoMapper;
import com.ra.pojo.UserAuth;
import com.ra.pojo.UserInfo;
import com.ra.pojo.dto.UserDetailsDTO;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Author : 今晚几点睡_Ada
 * Date: 2023/3/10
 * Time: 13:44
 * 注:
 */
@Service("userDetailServiceImpl")
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private UserAuthMapper userAuthMapper;
    @Autowired
    private RoleMapper roleMapper;



    @SneakyThrows
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username)) {
            throw new BizException("用户名不能为空!");
        }
        UserDetailsDTO user = userAuthMapper.getUserByUserName(username);
        if (user==null) {
            throw new BizException("用户不存在!");
        }
        List<String> roles = roleMapper.listRolesByUserInfoId(user.getUserInfoId());
        user.setRoles(roles);
        //System.out.println("userByUserName = " + user);
        return user;
        //return convertUserDetail(userByUserName, request);
    }
  /*  //转化UserDetail
    public UserDetailsDTO convertUserDetail(UserDetailsDTO user, HttpServletRequest request) {
        System.out.println("user = " + user);
        List<String> roles = roleMapper.listRolesByUserInfoId(user.getUserInfoId());
        return UserDetailsDTO.builder()
                .id(user.getId())
                .loginType(user.getLoginType())
                .username(user.getUsername())
                .password(user.getPassword())
                .roles(roles)
                .lastLoginTime(new Date())
                .build();
    }*/

}
